package com.yanshaohui.springboot.mapper

import org.apache.ibatis.annotations.Delete
import org.apache.ibatis.annotations.Insert
import org.apache.ibatis.annotations.InsertProvider
import org.apache.ibatis.annotations.Mapper
import org.apache.ibatis.annotations.Options
import org.apache.ibatis.annotations.Param
import org.apache.ibatis.annotations.Select
import org.apache.ibatis.annotations.SelectKey
import org.apache.ibatis.annotations.SelectProvider
import org.apache.ibatis.annotations.Update
import org.apache.ibatis.annotations.UpdateProvider

import com.yanshaohui.springboot.entity.AddressEntity

@Mapper
interface AddressMapper {
	
//	insert，返回值是：新插入行的主键（primary key）；需要包含<selectKey>语句，才会返回主键，否则返回值为null。
//	update/delete，返回值是：更新或删除的行数；无需指明resultClass；但如果有约束异常而删除失败，只能去捕捉异常。
//	queryForObject，返回的是：一个实例对象或null；需要包含<select>语句，并且指明resultMap；
//	queryForList，返回的是：实例对象的列表；需要包含<select>语句，并且指明resultMap；
	
	@Select("SELECT * FROM ADDRESS WHERE name = #{name}")
	AddressEntity findByName(@Param("name") String name);
	
	@Select("SELECT * FROM ADDRESS WHERE id = #{id}")
	AddressEntity findById(@Param("id") Long id);

	@Select("SELECT address FROM ADDRESS WHERE name = #{name}")
	String findAddressByName(@Param("name") String name);

	@Insert("INSERT INTO ADDRESS(name, address, post ,tel) VALUES(#{name}, #{address}, #{post}, #{tel})")
	@Options(useGeneratedKeys = true, keyProperty = "id")
	void insert(AddressEntity address);
	
	@Insert("INSERT INTO ADDRESS(name, address, post ,tel) VALUES(#{name}, #{address}, #{post}, #{tel})")
	@Options(useGeneratedKeys = true, keyProperty = "id")
	void insert2(@Param("name") String name, @Param("address") String address, @Param("post") String post ,@Param("tel") String tel);
	
	@Insert("INSERT INTO ADDRESS(name, address, post ,tel) VALUES(#{name}, #{address}, #{post}, #{tel})")
	@Options(useGeneratedKeys = true, keyProperty = "id")
	void insert3(Map<String, Object> map);

	@Update("UPDATE ADDRESS SET name=#{name},address=#{address},post=#{post},tel=#{tel} WHERE id=#{id}")
	int update(AddressEntity address);

	//需要删除所有byName的缓存
	@Delete("DELETE FROM ADDRESS WHERE id =#{id}")
	int delete(Long id);
	
	@Delete("DELETE FROM ADDRESS WHERE name =#{name}")
	int deleteByName(String name);
	
	/////////////////////////以下为动态sql
	@SelectProvider(type = AddressDynaSqlProvider.class,method = "selectAddress")
	List<AddressEntity> dynaFind(AddressEntity entity);

	@InsertProvider(type = AddressDynaSqlProvider.class,method = "insertAddress")
	@Options(useGeneratedKeys = true, keyProperty = "id")
	void dynaInsert(AddressEntity entity);
	
	@UpdateProvider(type = AddressDynaSqlProvider.class,method = "updateAddress")
	int dynaUpdate(AddressEntity entity);
}
